package com.itjason.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itjason.dto.NationUploadDataDTO;
import com.itjason.dto.Question;
import com.itjason.dto.QuestionDTO;
import com.itjason.exceptionhandler.ResponseException;
import com.itjason.po.GameOption;
import com.itjason.po.OptionItem;
import com.itjason.po.RegionalDataPO;
import com.itjason.service.GameOptionService;
import com.itjason.mapper.GameOptionMapper;
import com.itjason.service.OptionItemService;
import com.itjason.service.WxNationalDialectPageService;
import com.itjason.service.WxRegionalDialectPageService;
import com.itjason.utils.MultipartFileToFileUtils;
import com.itjason.utils.TencentCOSFileUpload;
import com.itjason.vo.WxQuestion;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
* @author dengxiang
* @description 针对表【game_option】的数据库操作Service实现
* @createDate 2024-05-30 23:38:50
*/
@Service
public class GameOptionServiceImpl extends ServiceImpl<GameOptionMapper, GameOption>
    implements GameOptionService{

    private String QUESTION_CONTENT="下列选项那个正确";
    private String[] SELECTOR={"A","B","C","D","E","F","G","H"};

    @Resource
    private OptionItemService optionItemService;

    @Resource
    private GameOptionMapper gameOptionMapper;

    @Resource
    private WxNationalDialectPageService wxNationalDialectPageService;

    @Resource
    private WxRegionalDialectPageService regionalDialectPageService;

    /**
     * 实现自动组卷功能
     * @return
     */
    @Override
    public List<WxQuestion> getQuestions() {

        //* 事项题目封装和题目试卷组成
        List<GameOption> gameOptions = gameOptionMapper.selectList(null);
        List<GameOption> gameOptions1= RandomUtil.randomEles(gameOptions, 5);
        List<WxQuestion> questions=new ArrayList<>();
        // 同过打乱的顺序组成相关试卷
        for (GameOption gameOption:gameOptions1){
            if(questions.size()<=5){
                WxQuestion wxQuestion = new WxQuestion();
                List<OptionItem> optionItems=optionItemService.getOptions(gameOption.getId());
                int i=0;
                for (OptionItem optionItem:optionItems){
                    optionItem.setSelector(SELECTOR[i]);
                    i++;
                }
                wxQuestion.setId(gameOption.getId());
                wxQuestion.setTitle(gameOption.getContent());
                wxQuestion.setOptions(optionItems);
                wxQuestion.setVideoPath(gameOption.getVideoPath());
                wxQuestion.setCurrent(gameOption.getCurrentPath());
                questions.add(wxQuestion);
            }else {
                break;
            }
        }

        return questions;
    }

    @Override
    public boolean answer(OptionItem questionDTO) {
        GameOption gameOption = gameOptionMapper.selectById(questionDTO.getGameId());
        System.out.println("gameOption = " + gameOption);
        return questionDTO.getVidePath().equals(gameOption.getCurrentPath());

    }

    @Override
    public List<OptionItem> queryOptionItem(Integer id) {
        return optionItemService.queryOptionItem(id);
    }

    @Override
    public ResponseException uploadFile(NationUploadDataDTO nationUploadDataDTO) {
       return ResponseException.success();
    }

    @Override
    public ResponseException addQuestion(Question question) {
        GameOption gameOption=gameOptionMapper.insertGameOption(question.getGameOption());
        if(gameOption.getId()==null){
            return ResponseException.error("600","插入失败");
        }
        List<OptionItem> optionItems = question.getOptionItems();
        List<OptionItem> saveOptionItems=new ArrayList<>();
        for (OptionItem optionItem : optionItems) {
            optionItem.setGameId(optionItem.getId());
            saveOptionItems.add(optionItem);
        }
        int integer=optionItemService.saveOptionItem(saveOptionItems);

        return null;
    }

    @Override
    public ResponseException delete(Integer id) {
        int optionItemResult =optionItemService.delete(id);
        int questionResult = gameOptionMapper.deleteById(id);
        if(optionItemResult>0 && questionResult > 0){
            return ResponseException.success();
        }
        return ResponseException.error("600","删除失败");
    }

    @Override
    public ResponseException updateQuestionInfo(Question question) {
        int update = gameOptionMapper.update(question.getGameOption(), null);
        int result=optionItemService.updateOptionItems(question.getOptionItems());
        return null;
    }
}




